//
//  IconDetailController.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "IconDetailController.h"
#import "NetworkingHandlerOfBlock.h"
#import "ModelForIconDetail.h"
#import "CellForIconDetail.h"
#import "DataDetailsController.h"
#import <DKNightVersion.h>
#import "NetworkingHandle.h"
#import "UIColor+Wonderful.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface IconDetailController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic ,retain) NSMutableArray *arrayForKey;
@property (nonatomic, retain) NSMutableDictionary *tempDic;


@end

@implementation IconDetailController
- (void)dealloc {
    [_tableView release];
    [_seg release];
    [_array release];
    [_dic release];
    [_arrayForKey release];
    [_tempDic release];
    [_name release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
     self.seg.selectedSegmentIndex = 0;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.name;
    [self createSubview];
    [self handleData];
    
    
}

- (void)createSubview {
    
   self.dic = [NSMutableDictionary dictionary];
    self.tempDic = [NSMutableDictionary dictionary];
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"比赛", @"数据"]];
    self.seg.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height / 20);
    self.seg.selectedSegmentIndex = 0;
    [self.view addSubview:self.seg];
    [_seg release];
    [self.seg addTarget:self action:@selector(selectedAction:) forControlEvents:UIControlEventValueChanged];
    
    self.seg.tintColor = [UIColor turquoise];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + self.view.frame.size.height / 20, self.view.frame.size.width, self.view.frame.size.height - 113 - self.view.frame.size.height / 20) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellForIconDetail class] forCellReuseIdentifier:@"poolForIcon"];
    
}

- (void)selectedAction:(UISegmentedControl *) seg {
    
    switch (seg.selectedSegmentIndex) {
            
        case 1:
        {
            
            DataDetailsController *dataController = [[DataDetailsController  alloc] init];
            
            NSString *str = @"tennis,tennis_0,tennis_1,tennis_2,tennis_3,tennis_4,tennis_5";
            NSString *str4 = @"cba_1";
            
            dataController.ID = self.ID;
            
            if ([self.ID isEqualToString:@"4"] || [self.ID isEqualToString:@"10"]|| [self.ID isEqualToString:@"328"] || [self.ID isEqualToString:str4] || [self.ID isEqualToString:@"213"] || [self.ID isEqualToString:@"2"] || [self.ID isEqualToString:@"1"] || [self.ID isEqualToString:@"3"] || [self.ID isEqualToString:@"5"] || [self.ID isEqualToString:str]) {
                
                
                if ([self.ID isEqualToString:str]) {
                    dataController.urling = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=tennis&_sport_s_=bet007_tennis&_sport_a_=playerorder&start=0&end=10&category=atps&country=";
                    
                    dataController.urling2 = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=tennis&_sport_s_=bet007_tennis&_sport_a_=playerorder&start=0&end=10&category=wtas&country=";
                    
                    
                    
                } else if ([self.ID isEqualToString:str4]) {
                    
                    dataController.urling = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=basketball&_sport_s_=cba&_sport_a_=teamorder";
                    
                } else {
                    
                    
                    NSString *str2 = @"http://platform.sina.com.cn/sports_client/team_rank?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=football&_sport_s_=opta&_sport_a_=teamOrder&league_type=";
                    NSString *str3 = @"&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
                    
                    dataController.urling = [str2 stringByAppendingFormat:@"%@%@",self.ID,str3];
                    
                    
                    NSString *str5 = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=football&_sport_s_=opta&_sport_a_=playerorder&type=";
                    
                    NSString *str6 = @"&item=13&limit=5";
                    dataController.urlForShoot = [str5 stringByAppendingFormat:@"%@%@", self.ID, str6];
                    
                    
                    NSString *str7 = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=football&_sport_s_=opta&_sport_a_=playerorder&type=";
                    
                    NSString *str8 = @"&item=2&limit=5";
                    
                    dataController.urlForzhugong = [str7 stringByAppendingFormat:@"%@%@", self.ID, str8];
                    
                }
                
                
                [self.navigationController pushViewController:dataController animated:YES];
                [dataController release];
                
                
                break;
                
                
            } else {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有数据" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *alartOK = [UIAlertAction actionWithTitle:@"OK" style:0 handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                }];
                
                [alert addAction:alartOK];
                [self presentViewController:alert animated:YES completion:nil];
                
                
            }
            
        }
            
            
        default:
            
            
            break;
            
            
    }
    
}

- (void)handleData {
  
    self.array = [NSMutableArray array];
    
    self.arrayForKey = [NSMutableArray array];
    
    
    [NetworkingHandle handleWorkData:self.strUrl AndBody:nil value:nil handleBlock:^(id result) {
        
        NSDictionary *dic = [result objectForKey:@"result"];
        NSDictionary *dic2 = [dic objectForKey:@"data"];
        NSArray *array = [dic2 objectForKey:@"full"];
        
        // 遍历数组 将相应的 model 放入数组中,并添加新的键值对(key:日期, value: model)
        
        for (NSDictionary *dictt in array) {
            
            ModelForIconDetail *model = [[ModelForIconDetail alloc] init];
            [model setValuesForKeysWithDictionary:dictt];
            [self.dic setObject:model forKey:model.date];
            [self.array addObject:model];
            
            [model release];
        }
        
        // 将新字典中的所有 key 值加入到数组中 (比赛的日期)
        self.arrayForKey = [self.dic allKeys].mutableCopy;
        // 对比赛日期进行排列
        [self.arrayForKey sortUsingSelector:@selector(compare:)];
        
        // 再次建立新字典 根据日期存入不同的比赛
        for (int i = 0; i < self.arrayForKey.count; i++) {
            
            NSMutableArray *tempArr2 = [NSMutableArray array];
            
            for (NSDictionary *dicttt in self.array) {
                
                // 如果存放日期的数组下标跟当前遍历的数组中的字典 key 中的 value 相等就加入到数组中
                if (self.arrayForKey[i] == [dicttt valueForKey:@"date"]) {
                    [tempArr2 addObject:dicttt];
                }
                
            }
            
            // 目的: 添加新的键值对,将相同日期的 key 中的 value 放到同一个字典中
            [self.tempDic setObject:tempArr2 forKey:self.arrayForKey[i]];
            
        }
        
        [self.tableView reloadData];

        
        
    }];
    

}

/** 分区个数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.arrayForKey.count;

}

/** 分区的标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return [self.arrayForKey objectAtIndex:section];

}

/** 每个分区对应的行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSString *key = [self.arrayForKey objectAtIndex:section];
    NSArray *arr = [self.tempDic objectForKey:key];
    
    return  arr.count;
    
}

/** 返回每个 cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellForIconDetail *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForIcon"];
    
    NSString *key = [self.arrayForKey objectAtIndex:indexPath.section];
    NSArray *arr = [self.tempDic objectForKey:key];
    
   // ModelForIconDetail *model = [[ModelForIconDetail alloc] init];
  ModelForIconDetail  *model = [arr objectAtIndex:indexPath.row];
       [cell passModelForHot:model];
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回每行的高度
    return self.view.frame.size.height / 4;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
